package com.gsy.response;

import com.gsy.enumerate.BaseEnum;
import com.gsy.enumerate.SystemStatusEnum;
import com.gsy.utils.JsonUtils;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.ArrayUtils;

import java.beans.Transient;
import java.io.Serializable;
import java.util.Objects;

/**
 * 有count的响应类
 * @author lzy
 * @date 2020/5/18
 **/
public class GlobalResponse implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(name = "code", value = "状态码", dataType = "Integer", example = "1")
    private Integer code;//状态码
    @ApiModelProperty(name = "message", value = "状态对应信息", dataType = "String", example = " 获取用户信息成功")
    private String message;//状态对应信息
    @ApiModelProperty(name = "data", value = "请求的数据结果", dataType = "json")
    private Object data;//请求的数据结果
    @ApiModelProperty(name = "count", value = "数据条数", dataType = "json")
    private Long count;

    public GlobalResponse() {
    }

    public GlobalResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public GlobalResponse(BaseEnum baseEnum) {
        this.code = baseEnum.getCode();
        this.message = baseEnum.getRemarks();
    }

    /**
     * @param baseEnum 系统错误枚举
     * @param addStr 拼接错误信息
     */
    public GlobalResponse(BaseEnum baseEnum, String addStr) {
        this.code = baseEnum.getCode();
        this.message = baseEnum.getRemarks() + addStr;
    }

    /**
     * @param baseEnum 系统错误枚举
     * @param addStr 拼接错误信息
     * @param data 数据
     */
    public GlobalResponse(BaseEnum baseEnum, String addStr, Object data) {
        this.code = baseEnum.getCode();
        this.message = baseEnum.getRemarks() + addStr;
        this.data = data;
    }

    /**
     * @param baseEnum 状态枚举
     * @param data 数据
     */
    public GlobalResponse(BaseEnum baseEnum, Object data,Long count) {
        this.code = baseEnum.getCode();
        this.message = baseEnum.getRemarks();
        this.data = data;
        this.count = count;
    }

    /**
     * @return true 成功 false 其他
     */
    @Transient
    public boolean isSuccess() {
        return Objects.equals(this.code, SystemStatusEnum.SUCCESS.getCode());
    }

    /**
     * 构建业务错误信息返回结果
     * @param message 错误信息
     * @return
     */
    public static GlobalResponse buildError(String... message) {
        SystemStatusEnum fail = SystemStatusEnum.FAIL;
        if (ArrayUtils.isNotEmpty(message)) {
            return new GlobalResponse(fail.getCode(), message[0]);
        }
        return new GlobalResponse(fail);
    }

    /**
     * 构建业务错误信息返回结果
     * @param message 错误信息
     * @return
     */
    public static GlobalResponse buildParamError(String... message) {
        SystemStatusEnum fail = SystemStatusEnum.PARAMETER_ERROR;
        if (ArrayUtils.isNotEmpty(message)) {
            return new GlobalResponse(fail.getCode(), message[0]);
        }
        return new GlobalResponse(fail);
    }

    /**
     * 构建业务成功信息返回结果
     * @param message 成功信息
     * @return
     */
    public static GlobalResponse buildSuccess(String... message) {
        return buildSuccess(null, message);
    }

    /**
     * 构建业务成功信息返回结果
     * @param data 返回的数据
     * @param message 成功信息
     * @return
     */
    public static GlobalResponse buildSuccess(Object data, String... message) {
        SystemStatusEnum success = SystemStatusEnum.SUCCESS;
        if (ArrayUtils.isNotEmpty(message) && data != null) {
            return new GlobalResponse(success, "," + message, data);
        }
        if (ArrayUtils.isNotEmpty(message)) {
            return new GlobalResponse(success, "," + message[0]);
        }
        if (data != null) {
            return new GlobalResponse(success, "", data);
        }
        return new GlobalResponse(success);
    }

    /**
     * 构建业务成功信息返回结果
     * @param data 返回的数据
     * @return
     */
    public static GlobalResponse buildSuccessData(Object data) {
        return new GlobalResponse(SystemStatusEnum.SUCCESS, "", data);

    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return JsonUtils.formatFiledName(this);
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
